<?php
// 应用公共文件
/**
 * 成功状态
 * @param null $data
 * @return bool|string
 */
function SuccessJsonResult($data = null): bool|string
{
    try {
        $array = array(
            'code' => 200,
            'msg' => '请求成功',
            'data' => $data
        );

        return json_encode($array, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
    } catch (JsonException $e) {
        return false;
    }
}


/**
 * Error状态
 * @return bool|string
 */
function ErrorJsonResult(): bool|string
{
    try {
        $array = array(
            'code' => 400,
            'msg' => '请求失败',
            'data' => null
        );

        return json_encode($array, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
    } catch (JsonException $e) {
        return false;
    }
}

/**
 * Custom状态
 * @param int $code
 * @param string $msg
 * @param $data
 * @return bool|string
 */
function CustomJsonResult(int $code = 100, string $msg = '', $data = null): bool|string
{
    try {
        $array = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        );

        return json_encode($array, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
    } catch (JsonException $e) {
        return false;
    }
}
